﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using AmuTools;
using app.Models;

namespace app.Api
{
    public class Permission
    {
        /*
            权限格式(X为0-9的数字，模块码和操作码第一位不能为0)：
            附加模块操作权限：+[XXXX][XX] 加号+模块码+操作码
            禁用模块操作权限：-[XXXX][XX] 减号+模块码+操作码

            权限分用户权限和角色权限，用户权限高于角色权限
            用户权限有禁用权限，但是角色没有禁用权限
            超级管理员权限为+100010，不可更改
        */

        static public PermissionOperation Module(PERMISSIONMODULE module)
        {
            return new PermissionOperation(module);
        }

        // 检测是否有与权限码对应的权限，需要提供权限字符串、权限码作为参数
        static public bool Check(string permission, string code)
        {
            Regex r;
            Match m;

            // 检测是否为超级管理员
            r = new Regex(@"\+100010");
            m = r.Match(permission);
            if (m.Value != "") return true;

            // 禁用模块组检测
            r = new Regex(string.Format(@"\-{0}10", code.Substring(0, 4)));
            m = r.Match(permission);
            if (m.Value != "") return false;

            // 禁用权限检测
            r = new Regex(string.Format(@"\-{0}", code));
            m = r.Match(permission);
            if (m.Value != "") return false;

            // 附加模块组检测
            r = new Regex(string.Format(@"\+{0}10", code.Substring(0, 4)));
            m = r.Match(permission);
            if (m.Value != "") return true;

            // 附加权限检测
            r = new Regex(string.Format(@"\+{0}", code));
            m = r.Match(permission);
            if (m.Value != "") return true;

            return false;
        }

        // 将权限码添加到权限中，返回新的权限
        static public string Add(string permission, string code)
        {
            // 检测是否为超级管理员
            if (permission == "+100010") return permission;

            // 删除权限
            permission = permission.Replace("-" + code, "");
            permission = permission.Replace("+" + code, "");
            // 添加权限
            permission = permission + "+" + code;
            return permission;
        }

        // 禁用某个权限，返回新的权限
        static public string Forbidden(string permission, string code)
        {
            // 检测是否为超级管理员
            if (permission == "+100010") return permission;

            // 删除权限
            permission = permission.Replace("-" + code, "");
            permission = permission.Replace("+" + code, "");
            // 添加权限
            permission = permission + "-" + code;
            return permission;
        }

        // 数组转为权限字符串
        static public string ArrayToPermission(string[] codes)
        {
            return "+" + string.Join("+", codes);
        }

        // 获取管理员权限
        static public string GetAdminPermission(string id)
        {
            Admin admin = G.DB.GetById<Admin>(id);
            SqlResult result = G.DB.Get(string.Format("select * from {0} where '{1}' like '%'+convert(varchar,id)+'%'", G.DB.GetTableName<AdminRole>(), admin.role));
            List<AdminRole> roles = result.GetFirstTableList<AdminRole>();
            string permission = "";
            foreach (AdminRole item in roles)
            {
                permission += item.permission;
            }
            return permission + admin.permission;
        }
    }

    public class PermissionOperation
    {
        public PERMISSIONMODULE ModuleCode { get; private set; }
        public PERMISSIONOPERATION OperationCode { get; private set; }
        public PermissionOperation(PERMISSIONMODULE module)
        {
            ModuleCode = module;
        }
        public PermissionOperation Operation(PERMISSIONOPERATION operation)
        {
            OperationCode = operation;
            return this;
        }
        public PermissionOperation Any()
        {
            return Operation(PERMISSIONOPERATION.ANY);
        }
        public PermissionOperation All()
        {
            return Operation(PERMISSIONOPERATION.ALL);
        }
        public PermissionOperation Add()
        {
            return Operation(PERMISSIONOPERATION.ADD);
        }
        public PermissionOperation Delete()
        {
            return Operation(PERMISSIONOPERATION.DELETE);
        }
        public PermissionOperation Modify()
        {
            return Operation(PERMISSIONOPERATION.MODIFY);
        }
        public PermissionOperation Look()
        {
            return Operation(PERMISSIONOPERATION.LOOK);
        }
        public PermissionOperation Check()
        {
            return Operation(PERMISSIONOPERATION.CHECK);
        }
        public PermissionOperation Import()
        {
            return Operation(PERMISSIONOPERATION.IMPORT);
        }
        public PermissionOperation Export()
        {
            return Operation(PERMISSIONOPERATION.EXPORT);
        }
        public PermissionOperation Upload()
        {
            return Operation(PERMISSIONOPERATION.UPLOAD);
        }
        public PermissionOperation SetPublic()
        {
            return Operation(PERMISSIONOPERATION.SETPUBLIC);
        }
        public PermissionOperation SaveTemplate()
        {
            return Operation(PERMISSIONOPERATION.SAVETEMPLATE);
        }
        public PermissionOperation BackupDatabase()
        {
            return Operation(PERMISSIONOPERATION.BACKUPDATABASE);
        }
        public PermissionOperation RestoreDatabase()
        {
            return Operation(PERMISSIONOPERATION.RESTOREDATABASE);
        }
        public PermissionOperation Download()
        {
            return Operation(PERMISSIONOPERATION.DOWNLOAD);
        }
        public PermissionOperation Sethonor()
        {
            return Operation(PERMISSIONOPERATION.SETHONOR);
        }
        public PermissionOperation SendMessage()
        {
            return Operation(PERMISSIONOPERATION.SENDMESSAGE);
        }
        public PermissionOperation Save()
        {
            return Operation(PERMISSIONOPERATION.SAVE);
        }
        public PermissionOperation OpenChcek()
        {
            return Operation(PERMISSIONOPERATION.OPENCHECK);
        }
        public PermissionOperation SendMessages()
        {
            return Operation(PERMISSIONOPERATION.SENDMESSAGE);
        }
        public PermissionOperation Top()
        {
            return Operation(PERMISSIONOPERATION.TOP);
        }
        public PermissionOperation Managed()
        {
            return Operation(PERMISSIONOPERATION.MANAGED);
        }
        public PermissionOperation STARTUSING()
        {
            return Operation(PERMISSIONOPERATION.STARTUSING);
        }
        public string GetCode()
        {
            return ((int)ModuleCode).ToString() + (OperationCode == PERMISSIONOPERATION.ANY ? "" : ((int)OperationCode).ToString());
        }
        public string GetName()
        {
            return ModuleCode.ToString() + "_" + OperationCode.ToString();
        }
    }

    // 操作码
    public enum PERMISSIONOPERATION
    {
        ANY = 0, // 任意
        ALL = 10, // 所有
        ADD = 11, // 添加
        DELETE = 12, // 删除
        MODIFY = 13, // 修改
        LOOK = 14, // 查看
        CHECK = 15, // 审核
        IMPORT = 16, // 导入
        EXPORT = 17, // 导出
        UPLOAD = 18, // 上传
        SETPUBLIC = 19,//设为公开
        SAVETEMPLATE = 20,//保存模板
        BACKUPDATABASE = 21,//备份数据库
        RESTOREDATABASE = 22,//还原数据库
        DOWNLOAD = 23,//下载
        SETHONOR = 24,//封号
        SENDMESSAGE = 25,//发送消息
        SAVE = 26,//保存
        OPENCHECK = 27,//审核开关
        SENDMESSAGES = 28,//批量发送消息
        TOP = 29,//置顶
        MANAGED = 30,//标记为处理
        EXCLUSIVE = 31, // 排他，如登陆时候是否能将其他地方的登陆挤下线
        LOGIN = 32, // 登陆
        LOGOUT = 33, // 退出登陆
        SUBMIT = 34,//提交
        OPENSYSTEM = 35,//系统开关
        STARTUSING = 36,//启用
        NEWTEMPLATE = 37,//新建模板
        FINDBACKPASSWORD = 41,//普通用户找回密码
        REGISTER = 42,//注册
        UPDATEBROWSERFILES = 43,//更新浏览器文件

    }

    // 模块码
    public enum PERMISSIONMODULE
    {
        ALL = 1000,

        // 管理员模块，1-4开头
        AUTHORITY = 1100, // 权限管理,00结尾，为模块组
        ADMIN_ROLE = 1101, // 管理员角色
        MEMBER_ROLE = 1102, // 会员角色
        ADMIN_MANAGE = 1103, // 添加管理员
        MEMBER_MANAGE = 1104, // 会员管理
        ADMIN_UPDATEPASSWORD = 1105,//管理员重置密码
        PAGE_MANAGE = 1106,//模块设置
        INFO_LIST = 1107,//文章管理
        HELP_LIST = 1108,//帮助中心
        FIRM_SERVICE_CHECK = 1109,//企业信息审核
        SERVICE_CHECK = 1110,//服务信息审核
        DEMAND_CHECK = 1111,//需求信息审核
        MAINTAIN = 1112,//系统开关
        DICTIONARY_MANAGER = 1113,//字典
        SYSREM_VALUE = 1114,//系统信息设置
        EMAIL_VALUE = 1115,//邮箱信息设置
        MESSAGE_VALUE = 1116,//短信密码设置
        EMAIL_TEMPLATE = 1117,//邮箱模板
        MESSAGE_TEMPLATE = 1118,//短信模板
        INDEX_LOGO = 1119,//网站图片
        SYSTEM_INTRODUCTION = 1120,//系统介绍
        ADMIN_LOG = 1121,//管理员日志
        USER_LOG = 1122,//普通用户日志
        FEEDBACK_MANAGE = 1123,//问题反馈
        BACKUP_DATABASE = 1124,//备份数据库
        RESTORE_DATABASE = 1125,//还原数据库
        DELETE_DATABASE = 1126,//删除备份数据库
        DOWNLOAD_DATABASE = 1127,//下载备份数据库
        OPEN_CHECK = 1128,//审核开关
        RELEASE_PLATEFORMSERVICE = 1129,//发布平台服务
        PLATEFORMSERVICE_CHECK = 1130,//平台服务审核
        BROWSER_UPDATE = 1131,//浏览器上传
        SERVICE_REGISTER_CHECK = 1132,//服务机构注册信息审核
        LISTED_COMPANY_INFORMATION=1133, //上市企业信息
        ALL_PLATEFORM_SERVICE_APPLICATION= 1134, //全部平台服务申请
        USUAL_SERVICE = 1135,//常用服务
        STATISTICS = 1136,//常用服务

        // 登陆
        LOGIN = 1201,

       
        LISTED_COMPANY=1133,//上市企业信息
        // 会员模块，5-9开头

    }
}